Skip to content

DSA RISC-V: key generation checks g^q mod p = 1 once - #4530

Merged
alex merged 1 commit into
mainfrom
claude/perf-4141-dsa-keygen-riscv64
Nov 7, 2026
Merged

alex merged 1 commit into
mainfrom
claude/perf-4141-dsa-keygen-riscv64

Conversation

@alex

@alex alex commented Nov 7, 2026

Copy link
Copy Markdown
Member

Part of #4141 (finding 4 of the audit): the RISC-V port of #4411. This PR changes no Spec/ or TCB/ file and no Rust code outside the generated RISC-V crate.

What was slow

vg_dsa_generate_key checks the domain with vg_dsa_check_parameters, whose g^q mod p = 1 is a modular exponentiation (exponent q, modulus p). It then derived the key with vg_dsa_derive_key, which ran the whole domain check again (checkParams in Impl/Dsa/RISCV64/Derive.lean, g^q included) before computing g^x. So key generation did three exponentiations modulo p where two are enough.

What changed

  • Generate.deriveChecked (Impl/Dsa/RISCV64/Generate.lean, emitted as the internal function vg_dsa_derive_key_checked) is vg_dsa_derive_key for a domain that has already passed the checks. Of the domain checks it keeps only the sizes, which give q's significant octets. It then sets up the working spaces the way the checks leave them, with Prevalidated.setup (the same code prevalidated verification already uses). It checks x and y exactly as Derive.body does. Unlike x86-64's deriveChecked, it also skips the cheap checks of g. On RISC-V the setup code for that already existed and was proven, and the precondition guarantees the domain passes every one of those checks.
  • Contract. It is proven against the existing, reviewed deriveKeyDomainCheckedContract (deriveChecked_verified, Proof/Dsa/RISCV64/DeriveCheckedVerified.lean). The satisfiability witness is validKeyWitness's domain, so no new decide +kernel is needed. Key generation's call uses the register-level form dcContract (dContract ∧ checkParametersBytes of the domain bytes).
  • Proofs.
    • Correctness: deriveKey_correct is generalized over the domain check (deriveWith_ok, DeriveFn.lean), and both derivations instantiate it. setupParams_ok is setup_ok for a domain that passes ParamsOk (DeriveChecked.lean).
    • Constant time (DeriveCheckedCT.lean): derivation's levels up to the sizes' branch, then prevalidated verification's setup cuts, then body_relD.
    • Key generation (GKDerive.lean, GKFn.lean, GKCT.lean, GKCTCall.lean): the call now goes to vg_dsa_derive_key_checked. derivec_pre establishes its precondition from the domain check's result (chkOf s₀ = true, already tracked) and the unchanged domain bytes (Ctx.par).
  • vg_dsa_derive_key itself, which PrivateKey::from_private_components calls, is unchanged. The generated change: vg_dsa_generate_key's call target, and the new internal function.

Numbers

Benchmarks workflow (riscv64; dsa; groups dsa_generate_key_q[0-9]+; with OpenSSL; base = main 0691e88): https://github.com/pyca/verified-garbage/actions/runs/42406449689 (table to follow when it completes).

I could not measure locally: this session runs on an x86-64 Xeon with no RISC-V emulator.

Checks

  • Built GKVerified, DeriveCheckedVerified and Artifacts.Dsa.RISCV64, and every RISC-V DSA module they import, after rebasing on main 0691e88. EmitOne.lean --check Dsa.RISCV64 passes.
  • Every step of the Static checks job passes, as does ci/run_unit_tests.py. check_lean_public_imports.py is OK on the edited modules.
  • cargo fmt --check passes. cargo clippy --all-targets --target riscv64gc-unknown-linux-gnu -- -D warnings and cargo build --lib --release --target riscv64gc-unknown-linux-gnu pass. The RISC-V tests run in CI.

Generated by Claude Code

vg_dsa_generate_key checked the domain with vg_dsa_check_parameters, whose
g^q mod p = 1 is a modular exponentiation, then derived the key with
vg_dsa_derive_key, which checked the whole domain again, g^q included.

Generate.deriveChecked (vg_dsa_derive_key_checked, an internal function) is
vg_dsa_derive_key for a domain that passes the checks: of them it keeps only
the sizes, which give q's significant octets, and sets up the working spaces
as the checks leave them (Prevalidated.setup, as prevalidated verification
does); it checks x and y as Derive.body does. Key generation calls it once
its domain check has returned 1.

It is proven against the existing deriveKeyDomainCheckedContract
(Spec/Dsa/DomainChecked.lean, deriveChecked_verified), and on the registers
(dcContract: dContract and checkParametersBytes of the domain) for its call
from key generation, whose proof establishes the precondition from the
check's result and the unchanged domain bytes (derivec_pre). Its correctness
is the derivation's (deriveWith_ok, DeriveFn.lean, now generic over the
check of the domain), and its constant time composes derivation's levels
with prevalidated verification's setup. vg_dsa_derive_key is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGabzaozUPN18mLpEv
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 7, 2026
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 33df778 Nov 7, 2026
23 checks passed
@alex
alex deleted the claude/perf-4141-dsa-keygen-riscv64 branch November 7, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants